package work1;

import java.util.Scanner;

//07.24的作业：

public class studentsystem {

    public static void menu() {
        System.out.println("欢迎使用学生信息录入系统!");
        System.out.println("""
                请选择功能：
                1.添加学生信息
                2.修改学生信息
                3.删除学生信息
                4.查询所有的学生信息
                5.根据学号查询学生信息
                6.根据姓名查询学生信息
                7.根据学生成绩进行从大到小的排序展示
                8.退出系统""");
    }

    public static void main(String[] args) {
        int[] nos = new int[100]; // 最多有100个学生
        String[] names = new String[100];
        double[] scores = new double[100];
        Scanner scanner = new Scanner(System.in);
        while (true) {
            menu();
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    // 添加学生信息
                    int id;
                    System.out.print("请输入学号：");
                    while (true) {
                        id = scanner.nextInt();
                        // 检查学号范围
                        if (id < 1000 || id > 9999) {
                            System.out.println("学号格式不正确，请重新输入！");
                            continue;
                        }
                        // 检查学号是否重复
                        boolean b = false;
                        for (int no : nos) {
                            if (no == id) {
                                b=true;
                                break;
                            }
                        }
                        if (b){
                            System.out.println("学号已存在，请重新输入！");
                        }else {
                            break;
                        }
                    }

                    System.out.print("请输入姓名：");
                    String name = scanner.next();

                    System.out.print("请输入成绩：");
                    double score;
                    while (true) {
                        score = scanner.nextDouble();
                        // 检查成绩范围
                        if (score < 0 || score > 100) {
                            System.out.println("成绩输入不正确，请重新输入！");
                            continue;
                        }
                        break;
                    }
                    // 存储学生信息
                    for (int i = 0; i < nos.length; i++) {
                        if (nos[i] == 0) {
                            nos[i] = id;
                            names[i] = name;
                            scores[i] = score;
                            System.out.println("学生信息添加成功！");
                            break;
                        }
                    }
                    break;
                case 2:
                    while (true) {
                        System.out.println("请输入要修改的学生学号：");
                        int modifyId = scanner.nextInt();
                        boolean b = false;
                        for (int i = 0; i < nos.length; i++) {
                            if (nos[i] == modifyId) {
                                b = true;
                                System.out.print("请输入新的姓名：");
                                names[i] = scanner.next();

                                System.out.print("请输入新的成绩：");
                                while (true) {
                                    double newScore = scanner.nextDouble();
                                    // 检查成绩范围
                                    if (newScore < 0 || newScore > 100) {
                                        System.out.println("成绩格式不正确，请重新输入！");
                                        continue;
                                    }
                                    scores[i] = newScore;
                                    break;
                                }
                                System.out.println("学生信息修改成功！");
                                break;
                            }
                        }
                        if (b) {
                            break;
                        } else {
                            System.out.println("学号不存在，请重新输入！");
                        }
                    }
                    break;
                case 3:
                    System.out.println("请输入要删除的学生学号：");
                    while (true) {
                        boolean b = false;
                        int deleteId = scanner.nextInt();
                        for (int i = 0; i < nos.length; i++) {
                            if (nos[i] == deleteId) {
                                b = true;
                                nos[i] = 0; // 将学号设为0表示删除
                                names[i] = null; // 清空姓名
                                scores[i] = 0; // 清空成绩
                                System.out.println("学生信息删除成功！");
                                break;
                            }
                        }
                        if(b){
                            break;
                        } else {
                            System.out.println("学号不存在，请重新输入！");
                        }
                    }
                    break;
                case 4:
                    int count =0;
                    for (int no : nos) {
                        if (no != 0) {
                            count++;
                        }
                    }
                    if (count == 0) {
                        System.out.println("没有学生信息，请添加！");
                    }else {
                        System.out.println("所有学生信息如下：");
                        for (int i = 0; i < nos.length; i++) {
                            if (nos[i] != 0) {
                                System.out.println("学号：" + nos[i] + ", 姓名：" + names[i] + ", 成绩：" + scores[i]);
                            }
                        }
                    }
                    break;
                case 5:
                    System.out.println("请输入要查询的学生学号：");
                    while (true) {
                        int queryId = scanner.nextInt();
                        boolean b = false;
                        for (int i = 0; i < nos.length; i++) {
                            if (nos[i] == queryId) {
                                b = true;
                                System.out.println("学号：" + nos[i] + ", 姓名：" + names[i] + ", 成绩：" + scores[i]);
                                break;
                            }
                        }
                        if(b){
                            break;
                        } else {
                            System.out.println("学号不存在，请重新输入！");
                        }
                    }
                    break;
                case 6:
                    System.out.println("请输入要查询的学生姓名：");
                    while (true) {
                        String queryName = scanner.next();
                        boolean b = false;
                        for (int i = 0; i < nos.length; i++) {
                            if (names[i] != null && names[i].equals(queryName)) {
                                b = true;
                                System.out.println("学号：" + nos[i] + ", 姓名：" + names[i] + ", 成绩：" + scores[i]);
                                break;
                            }
                        }
                        if(b){
                            break;
                        } else {
                            System.out.println("姓名不存在，请重新输入！");
                        }
                    }
                    break;
                case 7:
                    System.out.println("根据学生成绩进行从大到小的排序展示");
                    int[] indices = new int[nos.length];
                    for (int i = 0; i < nos.length; i++) {
                        indices[i] = i;
                    }
                    for(int i=0;i<scores.length-1;i++){
                        for(int j=i+1;j<scores.length;j++){
                            if(scores[indices[i]]<scores[indices[j]]){
                                // 交换索引
                                int temp = indices[i];
                                indices[i] = indices[j];
                                indices[j] = temp;
                            }
                        }
                    }
                    //检查原始数组中学号是否有效
                    for (int index : indices) {
                        if (nos[index] != 0) {
                            System.out.println("学号：" + nos[index]
                                    + ", 姓名：" + names[index]
                                    + ", 成绩：" + scores[index]);
                        }
                    }
                    break;
                case 8:
                    System.out.println("已成功退出系统！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("无效的选择！");
            }
        }
    }
}

